package com.ft.oa.service.industryService.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ft.oa.domain.industrys.Industry;
import com.ft.oa.domain.industrys.IndustryContributor;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.mapper.industrysMapper.IndustryContributorMapper;
import com.ft.oa.mapper.industrysMapper.IndustryMapper;
import com.ft.oa.mapper.sys.SysUserMapper;
import com.ft.oa.service.industryService.IIndustryContributorService;
import com.ft.oa.utils.DateUtils;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 行业参与人Service业务层处理
 * 
 * @author baowz
 * @date 2021-12-30
 */
@Service
public class IndustryContributorServiceImpl implements IIndustryContributorService {
    @Autowired
    private IndustryContributorMapper industryContributorMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查看参与人信息
     * @author baowz
     */
    @Override
    public List<SysUser> participants(int industryid,String industryName){
        QueryWrapper<IndustryContributor> wrapper = new QueryWrapper<>();
        wrapper.eq("industry_id",industryid);
        wrapper.eq("dellpeople","0");
        List<IndustryContributor> industryContributors = industryContributorMapper.selectList(wrapper);
        List peoplelist = new ArrayList<>();
        industryContributors.forEach(Contributors ->{
            HashMap<String,Object> peopleMap = new HashMap<>();
            SysUser sysUser = sysUserMapper.selectById(Contributors.getUserId());
            //参与人用户名
            peopleMap.put("username",sysUser.getUsername());
            //参与人手机号
            peopleMap.put("phone",sysUser.getPhone());
            //参与人id
            peopleMap.put("id",sysUser.getId());
            //行业 参与人id
            peopleMap.put("peopleid",Contributors.getId());
            peopleMap.put("industryid",industryid);
            peopleMap.put("industryName",industryName);
            peoplelist.add(peopleMap);
        });
        return peoplelist;
    }

    /**
     * 新增行业参与人
     * @param
     */
    @Override
    public int addpeople(IndustryContributor industryContributor) {
        int succes = 0;
        List<String> peopleList = industryContributor.getPeopleList();
        for (String list:peopleList) {
            IndustryContributor contributor = new IndustryContributor();
            String industry = industryContributor.getIndustryId();
            // 如果行业id和用户id存在  则更新他的状态进行去重
            QueryWrapper<IndustryContributor> wrapper = new QueryWrapper<>();
            wrapper.eq("industry_id",industry);
            wrapper.eq("user_id",list);
            wrapper.eq("dellpeople","0");
            List<IndustryContributor> industryContributors = industryContributorMapper.selectList(wrapper);
            if (industryContributors.size()!=0){
                contributor.setDellpeople("0");
                industryContributorMapper.update(contributor,wrapper);
            }else{
                contributor.setUserId(list);
                contributor.setIndustryId(industry);
                int inserts = industryContributorMapper.insert(contributor);
                succes+=inserts;
            }
        }
        return succes;
    }

    @Override
    public int dellparticipants(int peopleid) {
        IndustryContributor industryContributor = new IndustryContributor();
        industryContributor.setDellpeople("1");
        QueryWrapper<IndustryContributor> wrapper = new QueryWrapper<>();
        wrapper.eq("id",peopleid);
        return industryContributorMapper.update(industryContributor,wrapper);
    }

}
